package com.demo.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;

@Configuration
public class RibbonConfig {

	@Bean
	@LoadBalanced // 带有负载均衡的
	public RestTemplate restTemplateBalance() {
		return new RestTemplate();
	}

	@Bean // 不带负载均衡
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}

	/* 负载均衡策略，多种，详情请查看源码 */
	@Bean
	public IRule ribbonRule() {
		// BestAvailableRule 选择并发最少的
		// RoundRobinRule 轮询
		return new RandomRule();// 随机策略
	}
}
